package com.hyt.it.ogt.controller.ykcj.exam;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.hyt.it.ogt.controller.base.BaseController;
import com.hyt.it.ogt.ykcj.common.annotation.Log;
import com.hyt.it.ogt.ykcj.common.core.domain.AjaxResult;
import com.hyt.it.ogt.ykcj.common.core.page.TableDataInfo;
import com.hyt.it.ogt.ykcj.common.enums.BusinessType;
import com.hyt.it.ogt.ykcj.common.enums.FileType;
import com.hyt.it.ogt.ykcj.common.utils.StringUtils;
import com.hyt.it.ogt.ykcj.domain.exam.ExamPostInfo;
import com.hyt.it.ogt.ykcj.domain.vo.ExamPostInfoReq;
import com.hyt.it.ogt.ykcj.domain.vo.ExamPostInfoRsp;
import com.hyt.it.ogt.ykcj.domain.vo.ImportPostInfoVo;
import com.hyt.it.ogt.ykcj.service.exam.IExamPostInfoService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiSort;

/**
 * 招考职位信息Controller
 *
 * @author admin
 * @date 2021-09-09
 */
@Api(tags = "exam.3.职位管理", value = "职位管理请求接口")
@ApiSort(value = 103)
@RestController
@RequestMapping("/exam/postInfo")
public class ExamPostInfoController extends BaseController {
    @Autowired
    private IExamPostInfoService examPostInfoService;

    /**
     * 查询招考职位信息列表
     */
    @ApiOperation("查询招考职位信息列表")
    @GetMapping("/list")
    public TableDataInfo list(ExamPostInfoReq examPostInfoReq) {
        startPage();
        List<ExamPostInfoRsp> list = examPostInfoService.selectExamPostInfoList(examPostInfoReq);
        return getDataTable(list);
    }


    /**
     * 下载导入职位模板
     */
    @ApiOperation("下载导入职位模板")
    @Log(title = "下载导入职位模板", businessType = BusinessType.DOWNLOAD)
    @GetMapping("/downloadTemplate")
    public AjaxResult downloadTemplate() {
        return examPostInfoService.downloadTemplate();
    }

    /**
     * 导入招考职位
     */
    @ApiOperation(value = "导入招考职位")
    @Log(title = "导入招考职位", businessType = BusinessType.IMPORT)
    @PostMapping("/importPostInfo")
    public AjaxResult importPostInfo(MultipartFile multipartFile, String examId) {
        //校验文件格式
        if(null == multipartFile){
            return AjaxResult.error("文件为空！");
        }
        String fileName = multipartFile.getOriginalFilename();
        if(StringUtils.isEmpty(fileName)){
            return AjaxResult.error("文件名为空！");
        }
        String fileType = fileName.substring(fileName.indexOf("."));
        List<ImportPostInfoVo> list = new ArrayList<>();

        if (FileType.FILE_TYPE_XLSX.getType().equals(fileType)
                || FileType.FILE_TYPE_XLS.getType().equals(fileType)) {
            list = examPostInfoService.impotExcelPostInfo(multipartFile, examId);
        } else
            return AjaxResult.error("文件类型错误！");

        if (null != list && list.size() != 0 ){
            return AjaxResult.error("操作失败", list);
        }

        return AjaxResult.success();
    }


    /**
     * 获取招考职位信息详细信息
     */
    @ApiOperation("获取招考职位信息详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {

        return AjaxResult.success(examPostInfoService.selectExamPostInfoById(id));
    }

    /**
     * 新增招考职位信息
     */
    @ApiOperation("新增招考职位信息")
    @Log(title = "招考职位信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ExamPostInfo examPostInfo) {
        String msg = checkPostInfo(examPostInfo);
        if (msg != null)
            return AjaxResult.error(msg);
        return toAjax(examPostInfoService.insertExamPostInfo(examPostInfo));
    }

    /**
     * 修改招考职位信息
     */
    @ApiOperation("修改招考职位信息")
    @Log(title = "招考职位信息", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public AjaxResult edit(@RequestBody ExamPostInfo examPostInfo) {
        String msg = checkPostInfo(examPostInfo);
        if (msg != null)
            return AjaxResult.error(msg);
        return toAjax(examPostInfoService.updateExamPostInfo(examPostInfo));
    }

    /**
     * 删除招考职位信息
     */
    @ApiOperation("删除招考职位信息")
    @Log(title = "招考职位信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        int temp = examPostInfoService.deleteExamPostInfoByIds(ids);
        if (temp == 0)
            return AjaxResult.error("存在关联考生成绩，删除失败");
        return AjaxResult.success();
    }


    //校验新增职位信息
    private String checkPostInfo(ExamPostInfo examPostInfo) {
        String msg = null;
        boolean flag = false;
        while (!flag) {
            if (StringUtils.isEmpty(examPostInfo.getExamId())) {
                msg = "考试id为空";
                break;
            }
            if (StringUtils.isEmpty(examPostInfo.getDwdm())) {
                msg = "单位代码未填写";
                break;
            }
            if (StringUtils.isEmpty(examPostInfo.getDwmc())) {
                msg = "单位名称未填写";
                break;
            }
            if (StringUtils.isEmpty(examPostInfo.getCjkscj())) {
                msg = "未选择参加考试层级";
                break;
            }
            if (StringUtils.isEmpty(examPostInfo.getZwmc())) {
                msg = "职位信息未填写";
                break;
            }
            if (StringUtils.isEmpty(examPostInfo.getZwdm())) {
                msg = "职位代码未填写";
                break;
            }
            if (StringUtils.isEmpty(examPostInfo.getZwmc())) {
                msg = "职位信息未填写";
                break;
            }
            if (StringUtils.isEmpty(examPostInfo.getZwlb())) {
                msg = "未选择职位类别";
                break;
            }
            if (StringUtils.isEmpty(examPostInfo.getZwcj())) {
                msg = "未选择职位层级";
                break;
            }
            if (StringUtils.isEmpty(examPostInfo.getLb())) {
                msg = "未选择类别";
                break;
            }
            if (StringUtils.isEmpty(examPostInfo.getMsrybl())) {
                msg = "面试人员比率未填写";
                break;
            }
            if (StringUtils.isEmpty(examPostInfo.getSfzzzynlcs())) {
                msg = "未选择是否组织专业能力测试";
                break;
            }
            if (examPostInfo.getZkrsMan() != null || examPostInfo.getZkrsWoman() != null) {
                if (examPostInfo.getZkrsMan() == null) {
                    msg = "招考人数男不能为空";
                    break;
                }
                if (examPostInfo.getZkrsWoman() == null) {
                    msg = "招考人数女不能为空";
                    break;
                }
                if (examPostInfo.getZkrs() != (examPostInfo.getZkrsMan() + examPostInfo.getZkrsWoman())) {
                    msg = "实招人数与男女招考人数之和不相等";
                    break;
                }
            }
            flag = true;
        }
        return msg;
    }

}
